package com.newcapec.shiro.exception;


import com.newcapec.shiro.constant.SystemCode;
import com.newcapec.shiro.utils.RestResponse;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(value = CustomException.class)
    public RestResponse handleCustomException(CustomException e){
        return RestResponse.error(e.getCode(), e.getMessage());
    }

    /**
     * 处理没有权限异常
     */
    @ExceptionHandler(value = AuthorizationException.class)
    public RestResponse handleAuthorizationException(AuthorizationException e){
        return RestResponse.error(SystemCode.AuthError.getCode(), SystemCode.AuthError.getMessage());
    }

    /**
     * 其他异常处理
     */
    @ExceptionHandler(value = Exception.class)
    public RestResponse handleException(Exception e){
        return RestResponse.error(500, e.getMessage());
    }
}
